#include "User.h"
#include <QStringList>

User::User() :
	_userId(-1),
	_userName("User"),
	_userSurname(""),
	_userSurname2(""),
	_userBirthdate(QDate(1900,1,1)),
	_company(""),
	_post(""),
	_separator("=^_^=")
{
}

User::User(QString data) :
	_separator("=^_^=")
{
	if(!fromString(data)) return;

	_userId = 0; /// TODO: add user generator
}

bool User::fromString(QString data)
{
	// string format:    Name=^_^=Surname=^_^=Surname2=^_^=birthdate=^_^=company=^_^=post
	// birthdate format: DD.MM.YYYY|B. B shows birthdate privacy

	QStringList params = data.split(_separator);
	if(params.count() != 6) return false;

	_userName = params.at(0);
	_userSurname = params.at(1);
	_userSurname2 = params.at(2);

	QStringList dateString = params.at(3).split("|");
	if(dateString.count() != 2) return false;
	_userBirthdate = QDate::fromString(dateString.at(0),"dd.mm.yyyy");
	_hideBirthdate = dateString.at(1) != "0";

	_company = params.at(4);
	_post = params.at(5);
	return true;
}

QString User::toString()
{
	QStringList list;

	list.append(_userName);
	list.append(_userSurname);
	list.append(_userSurname2);

	QStringList date;
	date.append(_userBirthdate.toString("dd.mm.yyyy"));
	date.append(_hideBirthdate ? "1" : "0");
	list.append(date.join("|"));

	list.append(_company);
	list.append(_post);

	return list.join(_separator);
}

int User::getId()
{
	return _userId;
}

void User::setState(User::UserState state)
{
	_state = state;
}

User::UserState User::getState()
{
	return _state;
}

QString User::getName()
{
	return _userName;
}
